<template>
    <div class="app-container">
        <!-- 搜索栏 -->
        <el-form :model="queryParams" :inline="true" v-show="showSearch">
            <el-form-item label="用户昵称">
                <el-input @keyup.enter="handleQuery" v-model="queryParams.keyword" style="width: 200px"
                    placeholder="请输入用户昵称" clearable />
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="queryParams.isCheck" placeholder="留言状态" clearable style="width: 200px">
                    <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 操作按钮 -->
        <el-row :gutter="10" class="mb15">
            <el-col :span="1.5">
                <el-button type="danger" plain :disabled="messageIdList.length === 0" icon="Delete"
                    @click="handleDelete(undefined)">批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain :disabled="messageIdList.length === 0" icon="Check"
                    @click="handleCheck(undefined)">批量通过</el-button>
            </el-col>
            <RightToolBar @changeShow="changeShowSearch" :showSearch="showSearch" @queryTable="getList"></RightToolBar>
        </el-row>
        <!-- 表格展示 -->
        <el-table border :data="messageList" @selection-change="handleSelectionChange" v-loading="loading">
            <!-- 表格列 -->
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <!-- 用户头像 -->
            <el-table-column prop="avatar" label="头像" align="center" width="100">
                <template #default="scope">
                    <img :src="scope.row.avatar" width="40" height="40"  class="radius"/>
                </template>
            </el-table-column>
            <!-- 留言人 -->
            <el-table-column prop="nickname" label="留言人" align="center" width="150"></el-table-column>
            <!-- 留言内容 -->
            <el-table-column prop="messageContent" label="留言内容" align="center"></el-table-column>
            <!-- IP地址 -->
            <el-table-column prop="ipAddress" label="ip地址" align="center" width="150"></el-table-column>
            <!-- IP来源 -->
            <el-table-column prop="ipSource" label="ip来源" align="center" width="170"></el-table-column>
            <!-- 留言状态 -->
            <el-table-column prop="isCheck" label="状态" align="center" width="90">
                <template #default="scope">
                    <el-tag v-if="scope.row.isCheck == 0" type="warning">审核中</el-tag>
                    <el-tag v-if="scope.row.isCheck == 1" type="success">通过</el-tag>
                </template>
            </el-table-column>
            <!-- 留言时间 -->
            <el-table-column prop="createTime" label="留言时间" align="center" width="190">
                <template #default="scope">
                    <div class="create-time">
                        <el-icon>
                            <clock />
                        </el-icon>
                        <span style="margin-left: 10px">{{ formattedDate(scope.row.createTime) }}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- 操作 -->
            <el-table-column label="操作" align="center" width="160">
                <template #default="scope">
                    <el-button type="success" class="el-icon-plus" size="mini" plain v-if="scope.row.isCheck == 0"
                        @click="handleCheck(scope.row.id)">
                        通过
                    </el-button>
                    <el-button   type="danger" class="el-icon-delete" size="mini" plain @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-if="count > 0" :total="count" :page="queryParams.current" :limit="queryParams.size"
            @pagination="getList" />
    </div>
</template>

<script>
import { formatDate } from "@/utils/date";
import {checkMessage, getMessageList, deleteMessage} from "@/api/message";

export default {
  data() {
    return {
    count: 0,
    showSearch: true,
    loading: false,
    queryParams: {
        current: 1,
        size: 10,
    },
    status: [
        {
            value: 0,
            label: "审核中",
        },
        {
            value: 1,
            label: "通过",
        },
    ],
    messageIdList:[],
    messageList: [],
    }
  },
  methods:{
    //日期格式化
    formattedDate(date) {
      return formatDate(date);
    },
    handleDelete(id){
      if(id == undefined){
        this.$confirm('此操作将批量删除选中留言, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMessage(this.messageIdList).then((response) => {
            if(response.code == 200)
            {
              this.$message({type:'success',message:'删除成功'})
              this.getList();
              this.messageIdList = []
            }
          });
        }).catch(() => {
          this.$message({type:'info',message:'已取消删除'})
        });
      }else{
        this.$confirm('此操作将删除该留言, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMessage([id]).then((response) => {
            if(response.code == 200)
            {
              this.$message({type:'success',message:'删除成功'})
              this.getList();
            }
          });
        }).catch(() => {
          this.$message({type:'info',message:'已取消删除'})
        });
      }
    },
    handleCheck(id){
      if(id == undefined)
      {
        this.$confirm('此操作将批量通过选中留言, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          checkMessage({idList:this.messageIdList,isCheck:1}).then((response) => {
            if(response.code == 200)
            {
              this.$message({type:'success',message:'审核成功'})
              this.getList();
              this.messageIdList = []
            }
          });
        }).catch(() => {
          this.$message({type:'info',message:'已取消审核'})
        });
      }else
      {
        this.$confirm('此操作将通过该留言, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.messageIdList.push(id)
          checkMessage({idList:this.messageIdList,isCheck:1}).then((response) => {
            if(response.code == 200)
            {
              this.$message({type:'success',message:'审核成功'})
              this.getList();
            }
          });
        }).catch(() => {
          this.$message({type:'info',message:'已取消审核'})
        });
      }
    },
    handleQuery(){
      this.getList();
    },
     //多选处理
    handleSelectionChange(selectedItems) {
     this.messageIdList = selectedItems.map((item) => item.id);
    },
    queryTable(){},
    getList(){
      this.loading = true;
      getMessageList(this.queryParams).then((response) => {
        if(response.code == 200)
        {
        this.messageList = response.data.recordList;
        this.count = response.data.count;
        this.loading = false;
        }
      });
    },
    //改变搜索框显示状态
    changeShowSearch(data){
      this.showSearch = data;
    },
  },
  mounted() {
    this.getList();
  },
}
</script>

<style>
.radius{
  border-radius: 30%;
}
</style>